package map.android.baidu.carpool.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import com.baidu.components.api.extra.APIProxy;
import map.android.baidu.carpool.R;

public class CarpoolAlertDialog extends Dialog {

	private View.OnClickListener mPositiveOnClickListener;
	private View.OnClickListener mNegativeOnClickListener;
	private TextView mTitleTv;
	private TextView mMessageTv;
	private TextView mCancleTv;
	private TextView mPositionTv;
	

	protected CarpoolAlertDialog(Context context) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.carpool_alert_dialog);
		mTitleTv = (TextView) findViewById(R.id.carpool_title);
		mCancleTv = (TextView) findViewById(R.id.carpool_negative_btn);
		mPositionTv = (TextView) findViewById(R.id.carpool_positive_btn);
	}
	
	private void apply(DialogParams dialogParams) {
		if (TextUtils.isEmpty(dialogParams.mTitle)) {
			mTitleTv.setVisibility(View.GONE);
		} else {
			mTitleTv.setVisibility(View.VISIBLE);
			mTitleTv.setText(dialogParams.mTitle);
		}

		if (!TextUtils.isEmpty(dialogParams.mMessage)) {
			mMessageTv.setVisibility(View.VISIBLE);
			mMessageTv.setText(Html.fromHtml(dialogParams.mMessage));
		}

		if (!TextUtils.isEmpty(dialogParams.mNegativeButtonText)) {
			mCancleTv.setVisibility(View.VISIBLE);
			mCancleTv.setText(dialogParams.mNegativeButtonText);
			mCancleTv.setOnClickListener(mButtonClickListener);

		}
		if (!TextUtils.isEmpty(dialogParams.mPositiveButtonText)) {
			mPositionTv.setVisibility(View.VISIBLE);
			mPositionTv.setText(dialogParams.mPositiveButtonText);
			mPositionTv.setOnClickListener(mButtonClickListener);
		}

		mPositiveOnClickListener = dialogParams.mPositiveButtonListener;
		mNegativeOnClickListener = dialogParams.mNegativeButtonListener;

	}

	View.OnClickListener mButtonClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.carpool_negative_btn:
				if (mNegativeOnClickListener != null)
					mNegativeOnClickListener.onClick(mCancleTv);
				break;
			case R.id.carpool_positive_btn:
				if (mPositiveOnClickListener != null)
					mPositiveOnClickListener.onClick(mPositionTv);
				break;
			}
			dismiss();
		}
	};

	static class DialogParams {
		public final Context mContext;

		public int mIconId;
		public String mTitle;
		public String mMessage;
		public String mPositiveButtonText;
		public View.OnClickListener mPositiveButtonListener;
		public String mNegativeButtonText;
		public View.OnClickListener mNegativeButtonListener;
		public boolean mCancelable;

		DialogParams(Context mContext) {
			this.mContext = mContext;
		}
	}
	
	public static class Builder {
		private DialogParams dialogParams;

		public Builder(Context context) {
			dialogParams = new DialogParams(context);
		}

		public Builder setIcon(int iconId) {
			dialogParams.mIconId = iconId;
			return this;
		}

		public Builder setTitle(String title) {
			dialogParams.mTitle = title;
			return this;
		}

		public Builder setMessage(String message) {
			dialogParams.mMessage = message;
			return this;
		}

		public Builder setPositiveButton(int textId, final View.OnClickListener listener) {
			dialogParams.mPositiveButtonListener = listener;
			dialogParams.mPositiveButtonText = dialogParams.mContext.getString(textId);
			return this;
		}

		public Builder setPositiveButton(String text, final View.OnClickListener listener) {
			dialogParams.mPositiveButtonText = text;
			dialogParams.mPositiveButtonListener = listener;
			return this;
		}

		public Builder setNegativeButton(int textId, final View.OnClickListener listener) {
			dialogParams.mNegativeButtonText = dialogParams.mContext.getString(textId);
			dialogParams.mNegativeButtonListener = listener;
			return this;
		}

		public Builder setNegativeButton(String text, final View.OnClickListener listener) {
			dialogParams.mNegativeButtonText = text;
			dialogParams.mNegativeButtonListener = listener;
			return this;
		}

		public Builder setCancelable(boolean cancelable) {
			dialogParams.mCancelable = cancelable;
			return this;
		}

		public CarpoolAlertDialog create() {
			final CarpoolAlertDialog dialog = new CarpoolAlertDialog(dialogParams.mContext);
			dialog.apply(dialogParams);
			dialog.setCancelable(dialogParams.mCancelable);
			if (dialogParams.mCancelable) {
				dialog.setCanceledOnTouchOutside(true);
			}
			return dialog;
		}
	}

	@Override
	public void show() {
		super.show();
		ColorDrawable dw = new ColorDrawable(0x0000ff00);
		getWindow().setBackgroundDrawable(dw);
		int width = APIProxy.sys().getScreenWidth() * 14 / 15;
		getWindow().setLayout(width, -2);
	}
}